package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.UserMemberMapper;
import com.yh.homestay.pojo.UserMember;
import com.yh.homestay.service.UserMemberService;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserMemberServiceImpl implements UserMemberService {

    private Log logger = LogFactory.getLog(UserMemberServiceImpl.class);

    @Autowired
    private UserMemberMapper userMemberMapper;

    /**
     * @param page
     * @param limit
     * @param userName
     * @Date: 2020/1/13 12:02
     * @Description: 查询出所有用户信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult showUserMember(Integer page, Integer limit, String userName) {
        try {
            PageHelper.startPage(page, limit);
            List<UserMember> userMembers;
            if (StringUtils.isEmpty(userName)) {
                userMembers = userMemberMapper.selectAll();
            } else {
                Example example = new Example(UserMember.class);
                example.createCriteria().andLike("userName", "%" + userName + "%");
                userMembers = userMemberMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(userMembers);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param userMember
     * @Date: 2020/1/13 12:02
     * @Description: 插入用户信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult insertUserMember(UserMember userMember) {
        try {
            userMemberMapper.insertSelective(userMember);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/13 12:02
     * @Description: 删除用户信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult deleteUserMember(Integer id) {
        try {
            userMemberMapper.deleteByPrimaryKey(id);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param userMember
     * @Date: 2020/1/13 12:02
     * @Description: 更新用户信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult updateUserMember(UserMember userMember) {
        try {
            userMemberMapper.updateByPrimaryKeySelective(userMember);
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param id
     * @Date: 2020/1/13 12:02
     * @Description: 查询出一个用户信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult showOneUserMember(Integer id) {
        try {
            UserMember userMember = userMemberMapper.selectByPrimaryKey(id);
            return HomeStayResult.ok(200, userMember);
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
     * @param ids
     * @Date: 2020/1/13 12:03
     * @Description: 批量删除用户信息
     * @return: com.yh.homestay.common.HomeStayResultG
     */
    @Override
    public HomeStayResult deleteBatchUsers(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                userMemberMapper.deleteByPrimaryKey(id);
            }
            return HomeStayResult.ok();
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }
}